package src.dual_pointers;

import java.util.Arrays;

/**
 * @author starsea
 * @date 2024-08-19 9:57
 */

public class Test03 {
    public static void main(String[] args) {
        int[] arr={4,3,2,4};
        int c=triangleNumber(arr);
    }
    public static int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int ans=0;
        int n=nums.length;
        if(n<3)
        {
            return 0;
        }
        for(int i=n-1;i>=2;i--)
        {
            int tmp=nums[i];
            int left=0;
            int right=i-1;
            while(left<right)
            {
                if(nums[left]+nums[right]<=tmp)
                {
                    left++;
                }else{
                    ans+=right-left;
                    right--;
                }
            }

        }
        return ans;
    }
}
